
gl path "~/Dropbox/voter_id/Replication"

cap log close
log using "$path/table7_and_a10.log", replace

**** get the datasets ready to compute these values

use "$path/provisional_ballots.dta", clear
keep if general == 0
keep if year == 2016

rename pv_ethnicity ethnic_code
rename pv_race race_code
gen hispanic = ethnic_code == "HL"
replace hispanic = . if (race_code == "U" | race_code == "") & ethnic_code != "HL"
gen black = race_code == "B" & hispanic == 0 
replace black = . if (race_code == "U" | race_code == "") & ethnic_code != "HL"
gen white = race_code == "W" & hispanic == 0
replace white = . if (race_code == "U" | race_code == "")  & ethnic_code != "HL"
gen othernw = race_code != "U" & hispanic == 0 & black == 0 & white == 0 
replace othernw = . if (race_code == "U" | race_code == "")  & ethnic_code != "HL"
drop race_code ethnic_code

rename pv_party party_cd
gen dem = party_cd == "DEM"
gen rep = party_cd == "REP"
gen una = dem == 0 & rep == 0 
drop party_cd 

keep if reason == "ID NOT PROVIDED"
compress

tempfile provisional
save `provisional'

use id dem rep hispanic black white othernw ///
	birth_year voted no_dmv_match new_reg election using "$path/nc_dataset.dta" if (election == 9 | election == 10) & new_reg == 0, clear
	
egen non_miss = rownonmiss(hispanic black white othernw birth_year voted)
gen in_sample = non_miss == 6
drop non_miss
keep if in_sample
	
gen una = dem == 0 & rep == 0
compress

tempfile voterfile
save `voterfile'

**** 
* OVERALL EFFECT IN 2016 PRIMARY
****

// EFFECT = MECHANICAL + DETERRENT

	// MECHANICAL = MECHANICAL
	use `provisional', clear
	local mech = _N
	
	// DETERRENT = DETERRENT | NOID * TOTAL NOID
	use `voterfile', clear
	keep if election == 9 
	qui sum no_dmv_match 
	local total_noid = r(sum)
	di `total_noid'
	local det = 0.007 * `total_noid'
	di `mech'
	di `det'
	di round(`mech' + `det', 1)


**** 
* OVERALL EFFECT IN 2016 GENERAL
****

// EFFECT = DETERRENT

	// DETERRENT = DETERRENT | NOID * TOTAL NOID
	use `voterfile', clear
	keep if election == 10
	qui sum no_dmv_match 
	local total_noid = r(sum)
	di `total_noid'
	local det = 0.026 * `total_noid'
	di round(`det', 1)
	


****
* EFFECT DECOMPOSED BY RACE IN 2016 PRIMARY
****

// EFFECT = MECHANICAL + DETERRENT

	// MECHANICAL = MECHANICAL | NOID, WHITE +
				use `provisional', clear
				qui sum white
				local mech_white = r(mean) * _N
				di `mech_white'
				//	MECHANICAL | NOID, BLACK +
				qui sum black
				local mech_black = r(mean) * _N
				di `mech_black'
				//	MECHANICAL | NOID, HISPANIC  +
				qui sum hispanic
				local mech_hispanic = r(mean) * _N
				di `mech_hispanic'
				//  MECHANICAL | NOID, OTHERNW 
				qui sum othernw
				local mech_othernw = r(mean) * _N
				di `mech_othernw'
				
	// DETERRENT = DETERRENT | NOID, WHITE * TOTAL NOID, WHITE +
				use `voterfile', clear
				keep if election == 9 
				qui sum white if no_dmv_match
				local pct_white = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_white = `pct_white' * `total_noid'
				di `total_noid_white'
				local det_white = 0.007 * `total_noid_white'
				di `det_white'
				// DETERRENT | NOID, BLACK * TOTAL NOID, BLACK +
				qui sum black if no_dmv_match
				local pct_black = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_black = `pct_black' * `total_noid'
				di `total_noid_black'
				local det_black = 0.007 * `total_noid_black'
				di `det_black'
				// DETERRENT | NOID, HISPANIC * TOTAL NOID, HISPANIC +
				qui sum hispanic if no_dmv_match
				local pct_hispanic = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_hispanic = `pct_hispanic' * `total_noid'
				di `total_noid_hispanic'
				local det_hispanic = 0.007 * `total_noid_hispanic'
				di `det_hispanic'
				// DETERRENT | NOID, OTHERNW * TOTAL NOID, OTHERNW
				qui sum othernw if no_dmv_match
				local pct_othernw = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_othernw = `pct_othernw' * `total_noid'
				di `total_noid_othernw'
				local det_othernw = 0.007 * `total_noid_othernw'
				di `det_othernw'
				
	// EFFECT | WHITE = MECHANICAL | WHITE + DETERRENT | WHITE
	local effect_white = `mech_white' + `det_white'
	di round(`effect_white',1)
	// EFFECT | BLACK = MECHANICAL | BLACK + DETERRENT | BLACK
	local effect_black = `mech_black' + `det_black'
	di round(`effect_black',1)
	// EFFECT | HISPANIC = MECHANICAL | HISPANIC + DETERRENT | HISPANIC
	local effect_hispanic = `mech_hispanic' + `det_hispanic'
	di round(`effect_hispanic',1)
	// EFFECT | OTHERNW = MECHANICAL | OTHERNW + DETERRENT | OTHERNW
	local effect_othernw = `mech_othernw' + `det_othernw'
	di round(`effect_othernw',1)

	// TOTAL EFFECT
	local total_effect = `effect_white' + `effect_black' + `effect_hispanic' + `effect_othernw'
	di round(`total_effect',1)
	
	// COMPOSITION OF ELECTORATE
	drop if white == .
	
		// SHARE WHITE
		qui sum white if voted
		local white_share_law = r(mean)
		di `white_share_law'
		
		// SHARE BLACK
		qui sum black if voted
		local black_share_law = r(mean)
		di `black_share_law'
		
		// SHARE HISPANIC
		qui sum hispanic if voted
		local hispanic_share_law = r(mean)
		di `hispanic_share_law'
		
		// SHARE OTHER NW
		qui sum othernw if voted
		local othernw_share_law = r(mean)
		di `othernw_share_law'
		
		// ADD VOTE OVERALL EFFECT BY RACE BACK INTO THE ELECTORATE
		keep if voted
		qui count
		local plus = r(N) + round(`effect_white',1)
		set obs `plus'
		replace white = 1 if white == . 
		replace black = 0 if black == .
		replace hispanic = 0 if hispanic == .
		replace othernw = 0 if othernw == .
		replace voted = 1 if voted == .
		
		qui count
		local plus = r(N) + round(`effect_black',1)
		set obs `plus'
		replace white = 0 if white == . 
		replace black = 1 if black == .
		replace hispanic = 0 if hispanic == .
		replace othernw = 0 if othernw == .
		replace voted = 1 if voted == .
		
		qui count
		local plus = r(N) + round(`effect_hispanic',1)
		set obs `plus'
		replace white = 0 if white == . 
		replace black = 0 if black == .
		replace hispanic = 1 if hispanic == .
		replace othernw = 0 if othernw == .
		replace voted = 1 if voted == .
		
		qui count
		local plus = r(N) + round(`effect_othernw',1)
		set obs `plus'
		replace white = 0 if white == . 
		replace black = 0 if black == .
		replace hispanic = 0 if hispanic == .
		replace othernw = 1 if othernw == .
		replace voted = 1 if voted == .
		
		// SHARE WHITE
		qui sum white if voted
		local white_share_no_law = r(mean)
		di `white_share_no_law'
		
		// SHARE BLACK
		qui sum black if voted
		local black_share_no_law = r(mean)
		di `black_share_no_law'
		
		// SHARE HISPANIC
		qui sum hispanic if voted
		local hispanic_share_no_law = r(mean)
		di `hispanic_share_no_law'
		
		// SHARE OTHER NW
		qui sum othernw if voted
		local othernw_share_no_law = r(mean)
		di `othernw_share_no_law'
	

****
* EFFECT DECOMPOSED BY RACE IN 2016 GENERAL
****

// EFFECT =  DETERRENT

	// DETERRENT = DETERRENT | NOID, WHITE * TOTAL NOID, WHITE +
				use `voterfile', clear
				keep if election == 10 
				qui sum white if no_dmv_match
				local pct_white = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_white = `pct_white' * `total_noid'
				di `total_noid_white'
				local det_white = 0.026 * `total_noid_white'
				di `det_white'
				// DETERRENT | NOID, BLACK * TOTAL NOID, BLACK +
				qui sum black if no_dmv_match
				local pct_black = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_black = `pct_black' * `total_noid'
				di `total_noid_black'
				local det_black = 0.026 * `total_noid_black'
				di `det_black'
				// DETERRENT | NOID, HISPANIC * TOTAL NOID, HISPANIC +
				qui sum hispanic if no_dmv_match
				local pct_hispanic = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_hispanic = `pct_hispanic' * `total_noid'
				di `total_noid_hispanic'
				local det_hispanic = 0.026 * `total_noid_hispanic'
				di `det_hispanic'
				// DETERRENT | NOID, OTHERNW * TOTAL NOID, OTHERNW
				qui sum othernw if no_dmv_match
				local pct_othernw = r(mean)
				qui sum no_dmv_match
				local total_noid = r(sum)
				local total_noid_othernw = `pct_othernw' * `total_noid'
				di `total_noid_othernw'
				local det_othernw = 0.026 * `total_noid_othernw'
				di `det_othernw'

				// TOTAL EFFECT
				local total_effect = `det_white' + `det_black' + `det_hispanic' + `det_othernw'
				di round(`total_effect',1)
				
		// COMPOSITION OF ELECTORATE
		drop if white == .

	
			// SHARE WHITE
			qui sum white if voted
			local white_share_law = r(mean)
			di `white_share_law'
			
			// SHARE BLACK
			qui sum black if voted
			local black_share_law = r(mean)
			di `black_share_law'
			
			// SHARE HISPANIC
			qui sum hispanic if voted
			local hispanic_share_law = r(mean)
			di `hispanic_share_law'
			
			// SHARE OTHER NW
			qui sum othernw if voted
			local othernw_share_law = r(mean)
			di `othernw_share_law'

			// ADD VOTE OVERALL EFFECT BY RACE BACK INTO THE ELECTORATE
			keep if voted
			qui count
			local plus = r(N) + round(`det_white',1)
			set obs `plus'
			replace white = 1 if white == . 
			replace black = 0 if black == .
			replace hispanic = 0 if hispanic == .
			replace othernw = 0 if othernw == .
			replace voted = 1 if voted == .
			
			qui count
			local plus = r(N) + round(`det_black',1)
			set obs `plus'
			replace white = 0 if white == . 
			replace black = 1 if black == .
			replace hispanic = 0 if hispanic == .
			replace othernw = 0 if othernw == .
			replace voted = 1 if voted == .
			
			qui count
			local plus = r(N) + round(`det_hispanic',1)
			set obs `plus'
			replace white = 0 if white == . 
			replace black = 0 if black == .
			replace hispanic = 1 if hispanic == .
			replace othernw = 0 if othernw == .
			replace voted = 1 if voted == .
			
			qui count
			local plus = r(N) + round(`det_othernw',1)
			set obs `plus'
			replace white = 0 if white == . 
			replace black = 0 if black == .
			replace hispanic = 0 if hispanic == .
			replace othernw = 1 if othernw == .
			replace voted = 1 if voted == .
			
			// SHARE WHITE
			qui sum white if voted
			local white_share_no_law = r(mean)
			di `white_share_no_law'
			
			// SHARE BLACK
			qui sum black if voted
			local black_share_no_law = r(mean)
			di `black_share_no_law'
			
			// SHARE HISPANIC
			qui sum hispanic if voted
			local hispanic_share_no_law = r(mean)
			di `hispanic_share_no_law'
			
			// SHARE OTHER NW
			qui sum othernw if voted
			local othernw_share_no_law = r(mean)
			di `othernw_share_no_law'
	

****
* EFFECT DECOMPOSED BY PARTY IN 2016 PRIMARY
****

// EFFECT = MECHANICAL + DETERRENT
	// MECHANICAL = MECHANICAL | NOID, DEM +
				use `provisional', clear
				qui sum dem
				local mech_dem = r(mean) * _N
				di `mech_dem'
				//	MECHANICAL | NOID, REP +
				qui sum rep
				local mech_rep = r(mean) * _N
				di `mech_rep'
				//	MECHANICAL | NOID, UNA
				qui sum una
				local mech_una = r(mean) * _N
				di `mech_una'


	// DETERRENT = DETERRENT | NOID, DEM * TOTAL NOID, DEM +
				use `voterfile', clear
				keep if election == 9 
				qui sum no_dmv_match if dem
				local total_noid_dem = r(sum)
				di `total_noid_dem'
				local det_dem = 0.007 * `total_noid_dem'
				di `det_dem'
				// DETERRENT | NOID, rep * TOTAL NOID, rep +
				qui sum no_dmv_match if rep
				local total_noid_rep = r(sum)
				di `total_noid_rep'
				local det_rep = 0.007 * `total_noid_rep'
				di `det_rep'
				// DETERRENT | NOID, una * TOTAL NOID, una +
				qui sum no_dmv_match if una
				local total_noid_una = r(sum)
				di `total_noid_una'
				local det_una = 0.007 * `total_noid_una'
				di `det_una'
	

	// EFFECT | dem = MECHANICAL | dem + DETERRENT | dem
	local effect_dem = `mech_dem' + `det_dem'
	di round(`effect_dem',1)
	// EFFECT | rep = MECHANICAL | rep + DETERRENT | rep
	local effect_rep = `mech_rep' + `det_rep'
	di round(`effect_rep',1)
	// EFFECT | una = MECHANICAL | una + DETERRENT | una
	local effect_una = `mech_una' + `det_una'
	di round(`effect_una',1)

	// TOTAL EFFECT
	local total_effect = `effect_dem' + `effect_rep' + `effect_una'
	di round(`total_effect',1)			

	// COMPOSITION OF ELECTORATE
	drop if dem == .
	
		// SHARE DEM
		qui sum dem if voted
		local dem_share_law = r(mean)
		di `dem_share_law'
		
		// SHARE REP
		qui sum rep if voted
		local rep_share_law = r(mean)
		di `rep_share_law'
		
		// SHARE UNAFFIL
		qui sum una if voted
		local una_share_law = r(mean)
		di `una_share_law'
		
		
		// ADD VOTE OVERALL EFFECT BY RACE BACK INTO THE ELECTORATE
		keep if voted
		qui count
		local plus = r(N) + round(`effect_dem',1)
		set obs `plus'
		replace dem = 1 if dem == . 
		replace rep = 0 if rep == .
		replace una = 0 if una == .
		replace voted = 1 if voted == .
		
		qui count
		local plus = r(N) + round(`effect_rep',1)
		set obs `plus'
		replace dem = 0 if dem == . 
		replace rep = 1 if rep == .
		replace una = 0 if una == .
		replace voted = 1 if voted == .
		
		qui count
		local plus = r(N) + round(`effect_una',1)
		set obs `plus'
		replace dem = 0 if dem == . 
		replace rep = 0 if rep == .
		replace una = 1 if una == .
		replace voted = 1 if voted == .
	
		
		// SHARE dem
		qui sum dem if voted
		local dem_share_no_law = r(mean)
		di `dem_share_no_law'
		
		// SHARE rep
		qui sum rep if voted
		local rep_share_no_law = r(mean)
		di `rep_share_no_law'
		
		// SHARE una
		qui sum una if voted
		local una_share_no_law = r(mean)
		di `una_share_no_law'
		

****
* EFFECT DECOMPOSED BY PARTY IN 2016 GENERAL
****

// EFFECT =  DETERRENT

	// DETERRENT = DETERRENT | NOID, dem * TOTAL NOID, dem +
				use `voterfile', clear
				keep if election == 10 
				qui sum no_dmv_match if dem
				local total_noid_dem = r(sum)
				di `total_noid_dem'
				local det_dem = 0.026 * `total_noid_dem'
				di `det_dem'
				// DETERRENT | NOID, rep * TOTAL NOID, rep +
				qui sum no_dmv_match if rep
				local total_noid_rep = r(sum)
				di `total_noid_rep'
				local det_rep = 0.026 * `total_noid_rep'
				di `det_rep'
				// DETERRENT | NOID, una * TOTAL NOID, una +
				qui sum no_dmv_match if una
				local total_noid_una = r(sum)
				di `total_noid_una'
				local det_una = 0.026 * `total_noid_una'
				di `det_una'

				// TOTAL EFFECT
				local total_effect = `det_dem' + `det_rep' + `det_una'
				di round(`total_effect',1)

			// COMPOSITION OF ELECTORATE
			drop if dem == .
	
				// SHARE DEM
				qui sum dem if voted
				local dem_share_law = r(mean)
				di `dem_share_law'
				
				// SHARE REP
				qui sum rep if voted
				local rep_share_law = r(mean)
				di `rep_share_law'
				
				// SHARE UNAFFIL
				qui sum una if voted
				local una_share_law = r(mean)
				di `una_share_law'
				
				
				// ADD VOTE OVERALL EFFECT BY RACE BACK INTO THE ELECTORATE
				keep if voted
				qui count
				local plus = r(N) + round(`det_dem',1)
				set obs `plus'
				replace dem = 1 if dem == . 
				replace rep = 0 if rep == .
				replace una = 0 if una == .
				replace voted = 1 if voted == .
				
				qui count
				local plus = r(N) + round(`det_rep',1)
				set obs `plus'
				replace dem = 0 if dem == . 
				replace rep = 1 if rep == .
				replace una = 0 if una == .
				replace voted = 1 if voted == .
				
				qui count
				local plus = r(N) + round(`det_una',1)
				set obs `plus'
				replace dem = 0 if dem == . 
				replace rep = 0 if rep == .
				replace una = 1 if una == .
				replace voted = 1 if voted == .
			
				
				// SHARE dem
				qui sum dem if voted
				local dem_share_no_law = r(mean)
				di `dem_share_no_law'
				
				// SHARE rep
				qui sum rep if voted
				local rep_share_no_law = r(mean)
				di `rep_share_no_law'
				
				// SHARE una
				qui sum una if voted
				local una_share_no_law = r(mean)
				di `una_share_no_law'

log close
